/* 
 *  Copyright 2012 CodeMagi, Inc.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.codemagi.servlets.content.model;

import com.codemagi.login.model.IUser;
import java.util.*;

/**
 * Models a section of a site. Sections control who can edit page contents.
 *
 * @version 1.0
 * @author August Detlefsen for CodeMagi, Inc.
 */
public class Section extends com.codemagi.servlets.model.BaseBean implements ISection {

    private String name;
    private ArrayList teamMembers = new ArrayList();


    //GETTERS AND SETTERS -----------------------------------------------

    public void setName(String newValue) {
	name = newValue;
    }

    public String getName() {
	return name;
    }

    public void setTeamMembers(ArrayList newValue) {
	if (newValue != null) teamMembers = newValue;
    }

    public ArrayList getTeamMembers() {
	return teamMembers;
    }


    //SPECIAL GETTERS AND SETTERS ---------------------------------------

    /**                                                                                                               
     * Returns true if the passed User is part of the team for this Section. Should always return true if        
     * user is superuser.                                                                                 
     *                                                                                                             
     * @param user       The user to check for                                                                            
     * @return boolean   True if the user is a member of this Section, false otherwise
     */
    public boolean hasUser(IUser user) {
	if (user == null) return false;
	
	return (user.isSuperuser() || (user.isAuthenticated() && teamMembers.contains(user)) );
    }

    /**
     * Adds a user to this section.
     */
    public void addUser(IUser user) {
	if (!teamMembers.contains(user)) teamMembers.add(user);
    }


    /**
     * Removes a user from the section.
     */
    public void removeUser(IUser user) {
	teamMembers.remove(user);
    }

    /**
     * Removes a user from the section.
     */
    public void removeUser(Integer userId) {
	Iterator i = teamMembers.iterator();
	while (i.hasNext()) {
	    IUser user = (IUser)i.next();

	    if (user.getId().equals(userId)) teamMembers.remove(user);
	    break;
	}
    }

}
